// pages/Tabbar/User/User.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getUser } from '../../../api/user';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    assetsAddress: assetsAddress,
    CustomBar: app.globalData.CustomBar,
    titlePadRight: wx.getSystemInfoSync().screenWidth - wx.getMenuButtonBoundingClientRect().left,
    statusBarHeight: wx.getSystemInfoSync().statusBarHeight,
    user: {},
    entryList: [
        // { id: 1, name: '我的账户', icon: assetsAddress + '/my/billIcon.png', path: '/pages/Earnings/Balance/Balance'},
        { id: 2, name: '我的签名', icon: assetsAddress + '/my/signIcon.png', path: '/pages/Users/UserSignature/UserSignature'},
        { id: 3, name: '我的联系人', icon: assetsAddress + '/my/contactIcon.png', path: '/pages/Users/UserContact/UserContact'},
        { id: 4, name: '新手指南', icon: assetsAddress + '/my/guideIcon.png', path: '/pages/Systems/GuideNovice/GuideNovice'},
        { id: 5, name: '关于我们', icon: assetsAddress + '/my/abooutUsIcon.png', path: '/pages/Systems/AboutUs/AboutUs'},
        { id: 6, name: '用户协议', icon: assetsAddress + '/my/agreementIcon.png', path: '/pages/Systems/UserAgreement/UserAgreement'},
        // { id: 1, name: '账号注销', icon: assetsAddress + '/my/accountOffIcon.png', path: '/pages/Users/AccountRemove/AccountRemove'},
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
    onShow: function () {
        if(wx.getStorageSync('hasUpdateUser') == '1'){
            this.getUser();
            wx.setStorageSync('hasUpdateUser','')
        }
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUser();
    setTimeout(()=>{
        wx.stopPullDownRefresh({
          success: (res) => {
            console.log(res,'刷新成功')
            wx.showToast({
              title: '刷新成功',
              icon: 'none',
            })
          },
        })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 页面跳转
  goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
    }
  },
  // 获取用户个人信息
  getUser(){
    let _this = this;
    getUser()
    .then(res => {
      if(res.success){
        _this.setData({
          user: res.data,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
    // 退出登录
    loginOut(){
        wx.showModal({
            title: '温馨提示',
            content: '是否退出登录当前账号？',
            complete: (res) => {
                if (res.confirm) {
                    wx.clearStorageSync();
                    wx.exitMiniProgram({})
                }
            }
        })
    },
})